﻿using BusinessLayer;
using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using CustomerServiceCenter.Helper;
using CustomerServiceCenter.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace CustomerServiceCenter.Controllers
{
    public class TicketsController : Controller
    {
        public ActionResult TicketDetail()
        {
            return View();
        }

        [LoginAuthorize]
        public ActionResult TicketsRequestInfo()
        {
            return View();
        }

        [LoginAuthorize]
        [HttpGet]
        public ActionResult TicketDetail(int ticketId,int IsFromWeChat)
        {
            VTicketsRequestInfo model = TicketBusiness.GetTicketDetails(ticketId, IsFromWeChat);
            int assignerID = 0;
            string assignerName = null;
            string status = "New";
            string ticketNo = null;
            string projectName = "";
            if (model.AssignerInfo != null)
            {
                assignerID = Convert.ToInt32(model.AssignerInfo.ID);
                status = EnumDescripte.GetName(typeof(ShortStatus), model.Status);
            }
            long[] operatorId = new long[] { assignerID };
            List<VOperator> operatorInfo = ProjectBusiness.GetOperatorInfoById(operatorId);
            if (operatorInfo != null&&operatorInfo.Count>0)
            {
                assignerName = operatorInfo[0].Name;
            }
            if (model.IsFromWeChat == 2)
            {
                ticketNo = model.ReportTicketNo;
            }
            else
            {
                ticketNo = model.OrderNumber;
            }
            List<VApplication> list = ProjectBusiness.GetApplicationInfo(model.ApplicationID);
            if (null != list && list.Count > 0)
            {

                projectName = list[0].Name;
            }
            TicketDetailModel ticketDetail = new TicketDetailModel()
            {
                ID = model.TicketID,
                AssignerID = assignerID,
                AssignTo = assignerName,
                Client = model.Client.Name,
                Discription = model.IssueDescription,
                //IssueType = ProjectBusiness.GetIssueTypeInfoBySome(Convert.ToInt32(model.IssueType)).FirstOrDefault().IssueTypeDescription,
                IssueType = model.IssueTypeDescription,
                Project = projectName,
                Remarks = model.Comments,
                StateID = model.Status,
                State = status,
                TicketNo = ticketNo,
                //UserName = TicketBusiness.GetEndUserInfo(model.EndUserID).FirstOrDefault().Name,
                UserName = model.User.Name,
                ApplicationID = model.ApplicationID,
                ServiceComments =model.ServiceComments,
                IsFromWeChat = model.IsFromWeChat,
                Opened = model.SubmitDate,
                ResolvedTime = model.ResolveDate,
                ClosedTime = model.CloseDate,
                ClosedBy = assignerName,
                ResolvedBy = assignerName,
                Priority = EnumDescripte.GetName(typeof(TicketPrrority),model.Priority),
                PriorityNum = model.Priority,
                Email = model.Email,
                TEL = model.Mobile,
            };


            List<VTicketInputField> inputField =  TicketBusiness.GetTicketInputFieldBySome(model.ApplicationID);
            //List<VTicketInputFieldData> fieldData = TicketBusiness.GetFieldDataBySome(model.TicketID);
            
            List<InputFieldData> dataList = new List<InputFieldData>();
            foreach (var item in inputField)
            {
                InputFieldData data = new InputFieldData();
                List<InputFieldAndDatasourceDetail> detail = new List<InputFieldAndDatasourceDetail>();
                
                List<VTicketInputFieldData> fieldData = TicketBusiness.GetFieldDataBySome(model.TicketID, item.Id);
                List<VInputFieldDataSource> datasource =  TicketBusiness.GetInputDataSourceByFieldID(item.Id);
                DataSource dataSourceValue = new DataSource();
               
                if (fieldData.Count > 0)
                {

                    InputFieldAndDatasourceDetail inputFieldandDataSource = new InputFieldAndDatasourceDetail();
                    inputFieldandDataSource.EnglishName = item.EnglishName;
                    inputFieldandDataSource.ApplicationID = item.ApplicationID;
                    inputFieldandDataSource.FieldDataType = item.DataType;
                    inputFieldandDataSource.FieldDescription = item.Description;
                    inputFieldandDataSource.FieldName = item.Name;
                    inputFieldandDataSource.FieldSortSeq = item.SortSeq;
                    inputFieldandDataSource.IssueTypeID = item.IssueTypeID;
                    inputFieldandDataSource.IsMandary = item.IsMandary;
                    inputFieldandDataSource.FieldID = item.Id;
                    if (datasource.Count > 0 && datasource != null)
                    {

                        for (int i = 0; i < datasource.Count; i++)
                        {
                            if (fieldData[0].Value == datasource[i].value)
                            {
                                dataSourceValue.DataSourceDescription = datasource[i].Description;
                                dataSourceValue.DataSourceValue = datasource[i].value;
                                inputFieldandDataSource.InputDataSource = dataSourceValue;
                            }
                        }
                        data.Value = inputFieldandDataSource.InputDataSource.DataSourceDescription;
                    }
                    else
                    {
                        data.Value = fieldData[0].Value;
                    }
                    detail.Add(inputFieldandDataSource);
                    
                }

                data.InputField = detail;
                dataList.Add(data);
            }
            ticketDetail.Data = dataList;
            return View(ticketDetail);
        }


        //下载模板文件
        public void DownLoadTemp()
        {
            string filePath = ConfigurationManager.AppSettings["excelFilePath"].ToString();
            try
            {
                filePath = Server.MapPath(filePath);
                if (System.IO.File.Exists(filePath))
                {
                    FileInfo info = new FileInfo(filePath);
                    long fileSize = info.Length;
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
                    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + info.Name);
                    System.Web.HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
                    System.Web.HttpContext.Current.Response.WriteFile(filePath, 0, fileSize);
                    System.Web.HttpContext.Current.Response.Flush();
                }
            }
            catch
            { }
            finally
            {
                System.Web.HttpContext.Current.Response.Close();
            }
        }


    }
}
